package com.provue.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.provue.common.annotation.Log;
import com.provue.common.core.controller.BaseController;
import com.provue.common.core.domain.AjaxResult;
import com.provue.common.enums.BusinessType;
import com.provue.admin.domain.CommonUser;
import com.provue.admin.service.ICommonUserService;
import com.provue.common.utils.poi.ExcelUtil;
import com.provue.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author pro
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/admin/user")
public class CommonUserController extends BaseController
{
    @Autowired
    private ICommonUserService commonUserService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommonUser commonUser)
    {
        startPage();
        List<CommonUser> list = commonUserService.selectCommonUserList(commonUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:user:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommonUser commonUser)
    {
        List<CommonUser> list = commonUserService.selectCommonUserList(commonUser);
        ExcelUtil<CommonUser> util = new ExcelUtil<CommonUser>(CommonUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commonUserService.selectCommonUserById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('admin:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommonUser commonUser)
    {
        String result = commonUserService.insertCommonUser(commonUser);
        if(result.equals("操作成功")){
            return toAjax(1);
        }
        return error(result);
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('admin:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommonUser commonUser)
    {
        int result = commonUserService.updateCommonUser(commonUser);
        if(result == 501){
            return error("修改用户'" + commonUser.getNickName() + "'失败，登录账号已存在");
        } else if(result == 502){
            return error("修改用户'" + commonUser.getNickName() + "'失败，手机号码已存在");
        } else if (result == 503){

            return error("修改用户'" + commonUser.getNickName() + "'失败，邮箱账号已存在");
        }
        return toAjax(result);
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('admin:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int result = commonUserService.deleteCommonUserByIds(ids);
        if(result == 501){
            return error("不能删除当前登录用户");
        }
        return toAjax(result);
    }

}
